package models

import (
	"github.com/beego/beego/v2/client/orm"
	"time"
)

// Major 专业
type Major struct {
	Id int64
	Name string
	Desc string
	MajorNo string
	SuperFieldNo string
	CreateTime time.Time		`orm:"auto_now_add"`
	UpdateTime time.Time		`orm:"auto_now"`
	IsDelete bool				`orm:"default(0)"`
	Field *Field 				`orm:"rel(fk)"`
}

func (major *Major) TableName() string {
	return "t_major"
}

func NewMajor() *Major {
	return &Major{}
}

// Save 保存
func (major *Major) Save() (int64, error) {
	o := orm.NewOrm()
	return o.Insert(major)
}

// Update 更新指定字段
func (major *Major) Update(cols ...string) (int64, error) {
	o := orm.NewOrm()
	return o.Update(major, cols...)
}

// Delete 删除
func (major *Major) Delete() (int64, error) {
	o := orm.NewOrm()
	return o.Delete(major)
}

// DelLogic 逻辑删除
func (major *Major) DelLogic() error {
	if major.Id < 1 {
		return nil
	}
	o := orm.NewOrm()
	_, err := o.QueryTable(major).Filter(IdColumnName, major.Id).Update(orm.Params{
		"is_delete": 1,
	})
	return err
}

// GetById 根据id获取
func (major *Major) GetById() error {
	o := orm.NewOrm()
	return o.Read(major)
}